package cn.xm.lingshi.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.xm.lingshi.dao.CartDao;
import cn.xm.lingshi.dao.OrderDao;
import cn.xm.lingshi.dao.impl.CartDaoImpl;
import cn.xm.lingshi.dao.impl.OrderDaoImpl;
import cn.xm.lingshi.pojo.DingDan;
import cn.xm.lingshi.pojo.GouWuChe;
import cn.xm.lingshi.pojo.Shop;
import cn.xm.lingshi.service.OrderService;

public class OrderServiceImpl implements OrderService {
	OrderDao dao=new OrderDaoImpl();
	CartDao cdao=new CartDaoImpl();
	@Override
	public List<DingDan> queryRecord(int offer, int pageSize, int userid,
			int type){
		try {
			return dao.queryRecord( offer, pageSize,userid, type);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public int addOrder(GouWuChe cart, String address) {
		try {
			int num=dao.addOrder(cart, address);
			int num3=dao.queryOrderidByUserid(cart.getUserid());
			if(num==1){
				List<Shop> list=cart.getList();
				int num1=0;
				for (Shop shop : list) {
					int num2=dao.addOrderdetail(num3, shop.getId(), shop.getKucun());
					dao.queryKuCunByShopid(shop.getId(), shop.getShopnum());
					if(num2==1){
						num1++;
					}
				}
				if(list.size()==num1){
					cdao.delCartdetailByCartid(cart.getCartid());
					return 1;
				}
				return 0;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}

}
